package com.peternguyen.totolistpro;

import com.peternguyen.totolistpro.dao.*;
import com.peternguyen.totolistpro.data.Configapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

public class ActivityConfig extends Activity implements OnClickListener {
	ConfigDAO conDAO;
	Configapp conf, conf1,conf2;
	ImageButton btnSave;
	EditText txtValue;
	Spinner spnduedate;
	Spinner spnnewtoday;
	Spinner spnLanguage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_config);

		btnSave = (ImageButton) findViewById(R.id.btnConfigSave);
		spnduedate = (Spinner) findViewById(R.id.spnduedate);
		spnnewtoday = (Spinner) findViewById(R.id.spnnewDate);
		spnLanguage = (Spinner) findViewById(R.id.spnLanguage);

		btnSave.setOnClickListener(this);

		conDAO = new ConfigDAO(ActivityConfig.this);
		conf = conDAO.getConfig("numday_duedate");
		conf1 = conDAO.getConfig("newtoday");
		conf2 = conDAO.getConfig("language");
		

		if (conf == null) {
			conf = new Configapp();
			conf.set_name("numday_duedate");
			conf.set_value(String.valueOf(1));
			conDAO.Insert(conf);
		} else {
			spnduedate.setSelection(Integer.valueOf(conf.get_value()) - 1);
		}

		if (conf1 == null) {
			conf1 = new Configapp();
			conf1.set_name("newtoday");
			conf1.set_value(String.valueOf(1));
			conDAO.Insert(conf1);
		} else {
			spnnewtoday.setSelection(Integer.valueOf(conf1.get_value()) - 1);
		}
		
		if(conf2 != null){
			spnLanguage.setSelection(Integer.valueOf(conf2.get_value()));
		}
	}

	@Override
	public void onClick(View v) {
		int key = v.getId();
		switch (key) {
		case R.id.btnConfigSave:
			conDAO = new ConfigDAO(v.getContext());
			conf.set_value(String.valueOf(spnduedate.getSelectedItemPosition() + 1));
			conf1.set_value(String.valueOf(spnnewtoday.getSelectedItemPosition() + 1));
			conf2.set_value(String.valueOf(spnLanguage.getSelectedItemPosition()));
			conDAO.Update(conf);
			conDAO.Update(conf1);
			conDAO.Update(conf2);
			Toast.makeText(this, getString(R.string.update_sec),
					Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}

	}
}
